package com.qfedu.yanglao20admin.handicapped.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qfedu.yanglao20.common.constants.CommonConstants;
import com.qfedu.yanglao20.common.exeception.CommonException;
import com.qfedu.yanglao20.entity.equipment.FacilityManage;
import com.qfedu.yanglao20.entity.handicapped.Handicapped;
import com.qfedu.yanglao20.entity.handicapped.Relatives;
import com.qfedu.yanglao20admin.equipment.dao.FacilityManageDao;
import com.qfedu.yanglao20admin.handicapped.common.ServeStateEnum;
import com.qfedu.yanglao20admin.handicapped.dao.HandicappedDao;
import com.qfedu.yanglao20admin.handicapped.dao.RelativesDao;
import com.qfedu.yanglao20admin.handicapped.param.AddHandicappedParam;
import com.qfedu.yanglao20admin.handicapped.param.ContractParam;
import com.qfedu.yanglao20admin.handicapped.param.HandicappedParam;
import com.qfedu.yanglao20admin.handicapped.service.HandicappedService;
import com.qfedu.yanglao20admin.handicapped.vo.HandicappedVO;
import com.qfedu.yanglao20admin.handicapped.vo.SelectHandicappedVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author yujie
 * @data 2023/4/27 14:50
 */
@Service
public class HandicappedServiceImpl implements HandicappedService {

    @Resource
    private HandicappedDao handicappedDao;

    @Resource
    private RelativesDao relativesDao;

    @Resource
    private FacilityManageDao facilityManageDao;

    @Override
    public Page<HandicappedVO> pageHandicapped(HandicappedParam param) {
        // 计算偏移量
        Integer offset = (param.getPageNum() - 1) * param.getPageSize();
        param.setOffset(offset);

        // 分页查询
        Page<HandicappedVO> page = new Page<>(param.getPageNum(), param.getPageSize());

        List<HandicappedVO> handicappedVOS = handicappedDao.selectList(param);

        //  循环遍历
        // 使用枚举展示状态具体信息
        for (HandicappedVO vo :
                handicappedVOS) {
            ServeStateEnum stateById = ServeStateEnum.getStateById(vo.getStateFlag());
            vo.setStateDesc(stateById.getDesc());
        }

        // 页码
        page.setCurrent(param.getPageNum());
        // 每页展示记录数
        page.setSize(param.getPageSize());

        // 总记录数
        Long aLong = handicappedDao.selectCount(null);
        page.setTotal(aLong);

        page.setRecords(handicappedVOS);

        return page;
    }

    @Override
    public void addHandicapped(AddHandicappedParam addHandicappedParam) {

        if (addHandicappedParam.getName() == null || addHandicappedParam.getName().isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "请填写名称");
        }

        Handicapped handicapped =
                handicappedDao.selectByFacility(addHandicappedParam.getBoundFacility());
        if (handicapped != null) {
            throw new CommonException(CommonConstants.ERROR, "该设备已经绑定");
        }

        // 根据残疾人表中的信息设备名称查找设备信息
        FacilityManage facilityManage =
                facilityManageDao.selectByfacilityNum(addHandicappedParam.getBoundFacility());

        if (facilityManage != null) {
            if (facilityManage.getRunFlag() == 0) {
                addHandicappedParam.setBoundFacility(addHandicappedParam.getBoundFacility() + "离线");
            }
        }

        if (addHandicappedParam.getBoundFacility() == null || addHandicappedParam.getBoundFacility().isEmpty()) {
            addHandicappedParam.setBoundFacility("未绑定");
        }
        System.out.println(addHandicappedParam.getBoundFacility());

        if (facilityManage == null && !addHandicappedParam.getBoundFacility().equals("未绑定")) {
            throw new CommonException(CommonConstants.ERROR, "该设备不存在");
        }
        // 添加 残疾人信息
        handicappedDao.insertHandicapped(addHandicappedParam);


        // 通过判断，设置 ID 的值
        if (addHandicappedParam.getId() < 10) {
            addHandicappedParam.setAccountId("CJR00" + addHandicappedParam.getId());
        } else if (addHandicappedParam.getId() < 100 && addHandicappedParam.getId() >= 10) {
            addHandicappedParam.setAccountId("CJR0" + addHandicappedParam.getId());
        } else if (addHandicappedParam.getId() < 1000 && addHandicappedParam.getId() >= 100) {
            addHandicappedParam.setAccountId("CJR" + addHandicappedParam.getId());
        } else {
            addHandicappedParam.setAccountId("CJR" + addHandicappedParam.getId());
        }

        handicappedDao.updateaccountId(addHandicappedParam.getAccountId(), addHandicappedParam.getId());


        // 设置亲属关系中的 残疾人id
        List<Relatives> relativesList = addHandicappedParam.getRelativesList();
        for (Relatives list :
                relativesList) {
            list.setHandicappedId(addHandicappedParam.getId());
        }

        relativesDao.insertAll(relativesList);
    }

    @Override
    public void deleteHandicapped(Integer id) {
        // 根据 id 删除残疾人信息
        handicappedDao.deleteById(id);
    }

    @Override
    public void updateHandicapped(AddHandicappedParam addHandicappedParam) {

        if (addHandicappedParam.getName() == null || addHandicappedParam.getName().isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "请填写名称");
        }

        Handicapped handicapped =
                handicappedDao.selectByFacility(addHandicappedParam.getBoundFacility());
        if (handicapped != null) {
            if (!handicapped.getId().equals(addHandicappedParam.getId())) {
                throw new CommonException(CommonConstants.ERROR, "该设备已经绑定");
            }
        }

        // 根据残疾人表中的信息设备名称查找设备信息
        FacilityManage facilityManage =
                facilityManageDao.selectByfacilityNum(addHandicappedParam.getBoundFacility());

        if (facilityManage != null) {
            if (facilityManage.getRunFlag() == 0) {
                addHandicappedParam.setBoundFacility(addHandicappedParam.getBoundFacility() + "离线");
            }
        }

        if (facilityManage != null) {
            if (facilityManage.getRunFlag() == 1) {
                addHandicappedParam.setBoundFacility(addHandicappedParam.getBoundFacility());
            }
        }

        if (addHandicappedParam.getBoundFacility() == null || addHandicappedParam.getBoundFacility().isEmpty()) {
            addHandicappedParam.setBoundFacility("未绑定");
        }

        if (facilityManage == null && !addHandicappedParam.getBoundFacility().equals("未绑定")) {
            throw new CommonException(CommonConstants.ERROR, "该设备不存在");
        }

        handicappedDao.updateHandicapped(addHandicappedParam);

        relativesDao.updateAll(addHandicappedParam.getRelativesList());
    }

    @Override
    public void updateStateFlag(Integer stateFlag, Integer id) {
        // 修改服务状态
        Handicapped handicapped = handicappedDao.selectById(id);

        // 判断服务状态 未 0 改为 1
        if (handicapped.getStateFlag() == 0) {
            stateFlag = 1;
        } else if (handicapped.getStateFlag() == 1) {
            stateFlag = 0;
        }

        handicappedDao.updateStateFlag(stateFlag, id);
    }

    @Override
    public void updateContract(ContractParam contractParam) {
        // 修改合同日期
        if (contractParam.getStartTime().after(contractParam.getContractTime())) {
            throw new CommonException(CommonConstants.ERROR, "合同开始时间不能大于结束时间");
        }

        handicappedDao.updateContract(contractParam);

        Date date = new Date();

        // 判断合同时间是否 小于当前时间，是修改状态为合同到期
        if (contractParam.getContractTime().before(date)) {
            handicappedDao.updateContractState(contractParam.getId());
        }
    }

    @Override
    public SelectHandicappedVO selectAllById(Integer id) {
        return handicappedDao.selectAllById(id);
    }

    /**
     * 修改近照
     *
     * @param id
     */
    @Override
    public void updatePhotosUrl(String photosUrl, Integer id) {
        handicappedDao.updatePhotosUrl(photosUrl, id);
    }
}
